package com.software.car.service;

import com.github.pagehelper.PageHelper;
import com.software.car.api.UserService;
import com.software.car.common.enums.ResultEnum;
import com.software.car.common.exception.MyException;
import com.software.car.common.utils.CommonPage;
import com.software.car.common.utils.SnowflakeIdGenerator;
import com.software.car.entity.dto.request.UserInsertRequest;
import com.software.car.entity.dto.request.WeChatLoginRequest;
import com.software.car.entity.dto.request.WebLoginRequest;
import com.software.car.entity.dto.response.UserResponse;
import com.software.car.entity.po.UserPo;
import com.software.car.mapper.UserMessageMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author 魔法はまだ解けない
 * @Date 2021/1/23
 */
@Service
public class UserServiceImpl implements UserService {
    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    UserMessageMapper userMessageMapper;

    @Override
    public List<UserResponse> getAllUserResponse(int state) {
        return userMessageMapper.getAllUserResponse(state);
    }

    @Override
    public CommonPage getUsersByPage(Integer page, Integer pageSize,Integer state) {
        PageHelper.startPage(page, pageSize);
        List<UserResponse> allUserResponse = userMessageMapper.getAllUserResponse(state);
        CommonPage wrap = CommonPage.wrap(allUserResponse);
        return wrap;
    }

    @Override
    public UserResponse getUser(String id) {
        UserResponse user = userMessageMapper.getUser(id);
        return user;
    }

    @Override
    public UserPo getUserPo(String id) {
        UserPo user = userMessageMapper.getUserPo(id);
        return user;
    }

    @Override
    public UserResponse getUser(WebLoginRequest webLoginRequest) {
        UserResponse user = userMessageMapper.getUserByAccountPassword(webLoginRequest.getAccount(), webLoginRequest.getPassword());
        if (user == null) {
            throw new MyException(ResultEnum.LOGIN_EXCEPTION);
        }
        return user;
    }

    @Override
    public UserResponse getUser(WeChatLoginRequest weChatLoginRequest) {

        return null;
    }

    @Override
    public int insertUser(UserPo userPo) {
        return userMessageMapper.insertUser(userPo);
    }

    @Override
    public int insertUser(UserInsertRequest userInsertRequest) {
        UserPo userPo = UserPo.builder()
                .id(SnowflakeIdGenerator.getId())
                .name(userInsertRequest.getName())
                .account(userInsertRequest.getAccount())
                .password(userInsertRequest.getPassword())
                .role(userInsertRequest.getRole())
                .state(1)
                .sex(userInsertRequest.getSex())
                .updateTime(new Date())
                .createTime(new Date())
                .build();
        if (userInsertRequest.getEmail() != null) {
            userPo.setEmail(userInsertRequest.getEmail());
        }
        if (userInsertRequest.getPhone() != null) {
            userPo.setPhone(userInsertRequest.getPhone());
        }
        int i = insertUser(userPo);
        return i;
    }

    @Override
    public UserResponse getUserByAccount(String account) {
        UserResponse userByAccountPassword = userMessageMapper.getUserByAccountPassword(account, null);
        return userByAccountPassword;
    }

    @Override
    public UserResponse updateUser(UserPo updateUser, UserPo baseUser) {
        if (updateUser.getNickName() != null) {
            baseUser.setNickName(updateUser.getNickName());
        }
        if (updateUser.getName() != null) {
            baseUser.setName(updateUser.getName());
        }
        if (updateUser.getSex() != null) {
            baseUser.setSex(updateUser.getSex());
        }
        if (updateUser.getPassword() != null) {
            baseUser.setPassword(updateUser.getPassword());
        }
        if (updateUser.getAccount() != null) {
            baseUser.setAccount(updateUser.getAccount());
        }
        if (updateUser.getImg() != null) {
            baseUser.setImg(updateUser.getImg());
        }
        if (updateUser.getRole() != null) {
            baseUser.setRole(updateUser.getRole());
        }
        if (updateUser.getOpenid() != null) {
            baseUser.setOpenid(updateUser.getOpenid());
        }
        if (updateUser.getState() != null) {
            baseUser.setState(updateUser.getState());
        }
        if (updateUser.getEmail() != null) {
            baseUser.setEmail(updateUser.getEmail());
        }
        if (updateUser.getPhone() != null) {
            baseUser.setPhone(updateUser.getPhone());
        }
        baseUser.setUpdateTime(new Date());
        System.out.println(baseUser);
        int i = userMessageMapper.updateUser(baseUser);
        if(i==0){
            throw new MyException(ResultEnum.EXCEPTION);
        }
        UserResponse user = userMessageMapper.getUser(baseUser.getId());
        return user;
    }

    @Override
    public UserResponse userUpdateUser(UserPo updateUser, UserPo baseUser) {
        UserPo build = UserPo.builder()
                //baserUser的个人id
                .id(baseUser.getId())
                //下面是用户能修改的个人信息
                .name(updateUser.getName())
                .nickName(updateUser.getNickName())
                .sex(updateUser.getSex())
                .account(updateUser.getAccount())
                .img(updateUser.getImg())
                .email(updateUser.getEmail())
                .phone(updateUser.getPhone())
                .build();
        UserResponse userResponse = updateUser(build,baseUser);
        return userResponse;
    }

    @Override
    public UserResponse administerUpdateUser(UserPo updateUser) {
        UserPo build = UserPo.builder()
                .id(updateUser.getId())
                .nickName(updateUser.getNickName())
                .name(updateUser.getName())
                .sex(updateUser.getSex())
                .password(updateUser.getPassword())
                .account(updateUser.getAccount())
                .img(updateUser.getImg())
                .role(updateUser.getRole())
                .state(updateUser.getState())
                .email(updateUser.getEmail())
                .phone(updateUser.getPhone())
                .build();
        //获取用户信息
        UserPo userPo = getUserPo(updateUser.getId());
        return updateUser(build,userPo);
    }





}
